/**
 * Created with IntelliJ IDEA.
 * User: Katerina
 * Date: 14.01.13
 * Time: 23:04
 * To change this template use File | Settings | File Templates.
 */
package ru.gameChangers.homework.businessModels {
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;

import ru.gameChangers.homework.businessModels.ConfigurationConstants;

public class GridItem extends Sprite {

    private var currentIdx:int;
    private var isMouseOver:Boolean = false;
    private var animationPhase:int = 0;


    private var mouseFrame:GridItemFrame;
    private var body:GridItemBody;
    private var statusText:String;
    private var content:Array = new Array();
    private var statusLine:StatusLine;
    private var contentX:int;
    private var contentY:int;
    private var contentWidth:int;
    private var contentHeight:int;


    public function GridItem(x:int, y:int, width:int, height:int, statusText:String, contentContainer:ContentItemsContainer, statusLine:StatusLine) {

        setContentPosition(x, y,  width, height);
        contentContainer.setCallback(addNewLogo);

        this.statusText = statusText;
        this.statusLine = statusLine;

        body = new GridItemBody(x, y, width, height, ConfigurationConstants.FILL_COLOR);
        mouseFrame = new GridItemFrame(x, y, width, height, ConfigurationConstants.DARK_LINE_COLOR);
        addChild(body);

        addNewLogo(contentContainer.getTitleLogo());
        currentIdx = (Math.random() * 100) % content.length;
        content[currentIdx].logo.alpha = 1;

        this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
        this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
        this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }

    public function onMouseDown(e:Event):void {
        content[currentIdx].logo.alpha = 0;
        increaseCurrentIdx();
        animationPhase = 2;
        isMouseOver = true;
        this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    public function onEnterFrame(e:Event) : void {
        if (isMouseOver == false || animationStep() == false) {
            content[currentIdx].logo.alpha = 1;
            this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
    }

    public function onMouseOver(e:Event):void {
        isMouseOver = true;
        if (animationPhase == 2) {
            animationPhase = 1;
        }
        content[currentIdx].logo.alpha = 1;
        if (!contains(mouseFrame)) {
            addChild(mouseFrame);
        }
        statusLine.updateStatus(statusText, content[currentIdx].getDescription());
    }

    public function onMouseOut(e:Event):void {
        isMouseOver = false;
        if (contains(mouseFrame)) {
            removeChild(mouseFrame);
        }
        statusLine.close();
    }

    public function startPlay() : Boolean {
        if (isMouseOver == false) {
            animationPhase = 1;
            return true;
        }
        return false;
    }

    public function play() : Boolean {
        if (isMouseOver == false) {
            return animationStep();
        }
        return animationPhase > 0;
    }

    private function animationStep() : Boolean {
        if (animationPhase == 1) {
            content[currentIdx].logo.alpha -= 0.02;
            if (content[currentIdx].logo.alpha <= 0.1) {
                content[currentIdx].logo.alpha = 0;
                animationPhase = 2;
                increaseCurrentIdx();
            }
            return true;
        }
        if (animationPhase == 2) {
            content[currentIdx].logo.alpha += 0.02;
            if (content[currentIdx].logo.alpha >= 0.9) {
                content[currentIdx].logo.alpha = 1;
                animationPhase = 0;
                return false;
            }
            return true;
        }
        return false;
    }

    private function increaseCurrentIdx() : void {
        currentIdx = (currentIdx + 1) % content.length;
        if (isMouseOver == true) {
            statusLine.updateStatus(statusText, content[currentIdx].getDescription());
        }
    }

    private function addNewLogo(company:ContentItem) : void {
        content.push(company);
        addChild(formatImage(company.logo, contentX, contentY, contentWidth, contentHeight));
    }

    private function setContentPosition(x:int, y:int, width:int, height:int) {
        contentX = x;
        contentY = y;
        contentWidth = width;
        contentHeight = height;
    }

    private static function formatImage(img:DisplayObject, x:int, y:int, width:int, height:int) : DisplayObject {
        var imgScaleX:Number = 1;
        var imgScaleY:Number = 1;

        var decreasedWidth:int = width - width / 7;
        var decreasedHeight:int = height - height / 7;

        if (img.width > decreasedWidth) {
            imgScaleX = (decreasedWidth + 0.0) / (img.width + 0.0);
        }
        if (img.height > decreasedHeight) {
            imgScaleY = (decreasedHeight + 0.0) / (img.height + 0.0);
        }
        var imgScale:Number = Math.min(imgScaleX, imgScaleY);
        img.scaleX = imgScale;
        img.scaleY = imgScale;

        img.x = (width - img.width) / 2 + x;
        img.y = (height - img.height) / 2 + y;
        img.alpha = 0;

        return img;
    }

}
}
